﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using CalcSharp.Core;

namespace CalcSharp.Ui
{
    public class MenuCommandsParser: IDisposable
    {
        private string[] Supported;
        private Dictionary<string, string> DirParser;

        public MenuCommandsParser()
        {
            Supported = new string[]
            {
                "RUN"
            };
            DirParser = new Dictionary<string, string>();
            DirParser.Add("%WINDIR%", Environment.GetFolderPath(Environment.SpecialFolder.System));
            DirParser.Add("%PERSONAL%,", Environment.GetFolderPath(Environment.SpecialFolder.Personal));
        }

        public bool InterpretCommand(string Cmd)
        {
            string[] Command = MiscCoreFuncts.split(Cmd.ToUpper(), false);
            if (!Supported.Contains(Command[0])) return false;
            for (int i = 1; i < Command.Length; i++)
            {
                foreach (string rep in DirParser.Keys)
                {
                    if (Command[i].Contains(rep)) Command[i].Replace(rep, DirParser[rep]);
                }
            }
            switch (Command[0])
            {
                case "RUN":
                    this.RunFile(Command[1]);
                    return true;
                default:
                    return false;
            }
        }

        private void RunFile(string file)
        {
            Process P = new Process();
            P.StartInfo.UseShellExecute = true;
            P.StartInfo.FileName = file;
            P.Start();
        }

        #region IDisposable Members

        public void Dispose()
        {
            this.Supported = null;
            this.DirParser = null;
            GC.SuppressFinalize(this);
        }

        #endregion
    }
}
